dojo.require("dojo.io.script");
dojo.require("dijit.Dialog");

function Kollektiv() {
    Kollektiv = this;

	this.Request = function (route, controller, action, content, errorMessage, handle, parameter) {
		if (content == null) content = {};
		content.SESSION = Kollektiv.Session;
		dojo.xhrPost({
			url: "/" + route + "/" + controller + "/" + action,
			timeout: 5000,
			content: content,
			load: handle,
			error: function (error) { Kollektiv.ShowError(error + "<br/>" + errorMessage); },
			errorMessage: errorMessage,
			parameter: parameter
		});
	};

	this.RequestData = function (route, controller, action, content, errorMessage, handle, parameter) {
		Kollektiv.Request(route, controller, action, content, errorMessage, function (data) {
			handle(dojo.fromJson(data));
		}, parameter);
	};

	this.LoadJS = function (assemblyName, resourceKey, checkString, handle, parameter) {
		dojo.io.script.get({
			url: "/" + Kollektiv.BaseRoute + "/Base/JavaScriptResource",
			content: { assemblyName: assemblyName, resourceKey: resourceKey },
			checkString: checkString,
			load: handle,
			error: function (error) {
				Kollektiv.ShowError(error + "<br/>" + "Loading javascript file failed. assemblyName = " + this.content.assemblyName + "; resourceKey = " + this.content.resourceKey);
			},
			parameter: parameter
		});
	};

	this.ProcessRoutes = function (routes) {
		for (var route in routes) {
			var jss = routes[route]["js"];
			for (var js in jss) {
				var handler = eval("typeof(" + jss[js].handler + ")");
				if (handler == "undefined") {
					Kollektiv.LoadJS(jss[js].assemblyName, jss[js].resourceKey, jss[js].handler, function () {
						eval("new " + this.parameter.handler + "(); Kollektiv.RouteHandler[\"" + this.parameter.route + "\"] = " + this.parameter.handler + ";");
						Kollektiv.CreateRouteUI(this.parameter.route, this.parameter.name);
					}, { handler: jss[js].handler, route: route, name: jss[js].name });
				}
			}
		}
	};

	this.CreateRouteUI = function (route, name) {
		var isSearch = typeof (Kollektiv.RouteHandler[route].Search) != "undefined";
		var isTree = typeof (Kollektiv.RouteHandler[route].TreeModel) != "undefined";
		//	    var isList = typeof (Kollektiv.RouteHandler[route].ListModel) != "undefined";

		var accordionContentPaneContent = "<table id=\"accordionContentPaneTable_" + route + "\" class=\"accordionContentPaneTable\">";
		if (isSearch) accordionContentPaneContent = accordionContentPaneContent + "<tr><td id=\"accordionContentPaneSearch_" + route + "\"></td></tr>";
		if (isTree) accordionContentPaneContent = accordionContentPaneContent + "<tr><td id=\"accordionContentPaneTree_" + route + "\"></td></tr>";
		//	    if (isList) accordionContentPaneContent = accordionContentPaneContent + "<tr><td id=\"accordionContentPaneList_" + route + "\"></td></tr>";
		accordionContentPaneContent = accordionContentPaneContent + "</table>";

		var accordionContainer = dijit.byId("accordionContainer");
		var accordionContentPane = new dijit.layout.ContentPane({
			title: name,
			content: accordionContentPaneContent
		})
		accordionContainer.addChild(accordionContentPane);

		if (isSearch)
			new dijit.form.TextBox({
				name: "Search",
				placeHolder: "search...",
				style: "width: 100%",
				onChange: Kollektiv.RouteHandler[route].Search
			}, "accordionContentPaneSearch_" + route);

		if (isTree)
			new dijit.Tree({
				model: Kollektiv.RouteHandler[route].TreeModel,
				onClick: Kollektiv.RouteHandler[route].Select
			}, "accordionContentPaneTree_" + route);

		//        if(isList)
		//            new dijit.Tree({
		//                model: Kollektiv.RouteHandler[route].List,
		//                onClick: Kollektiv.RouteHandler[route].Select
		//            }, "accordionContentPaneList_" + route);
	};

	this.ShowError = function (message) {
		var contanctString = "Please contact" + (dojo.isString(this.AdministratorEmail) ? "<a href=\"mailto: " + this.AdministratorEmail + "\">Administrator</a>" : "Administrator");

		var dialog = new dijit.Dialog({
			title: "Error",
			content: message + "<br/>" + contanctString,
			style: "width: 500px"
		});

		dialog.show();
	};

	// globals
	this.BaseRoute = "$(BaseRouteID)";
	this.ClientProfile = "web";
	this.RouteHandler = {};

	// init
	this.RequestData(this.BaseRoute, "Session", "Ping", null, "Connecting to server failed.", function (data) {
		Kollektiv.Session = data;

		Kollektiv.RequestData(Kollektiv.BaseRoute, "Base", "GetClientConfig", { profile: Kollektiv.ClientProfile }, "Fetching client configuration failed.", function (data) {
			Kollektiv.PingIntervall = data.PingIntervall;

			for (var value in data.Values) {
				switch (data.Values[value].Key) {
					case "AdministratorEmail":
						Kollektiv.AdministratorEmail = data.Values[value].Value;
						break;
					case "Routes":
						Kollektiv.ProcessRoutes(dojo.fromJson(data.Values[value].Value));
						break;
				}
			}
		});
	});
};